

#include "Base.h"

#include "GameTime.h"
#include "Timer/Timer.h"

/*
#if defined(WIN32)

// Win32
#include <windows.h>


TimeVal getPerfTime()
{
	__int64 count;
	QueryPerformanceCounter((LARGE_INTEGER*)&count);
	return count;
}

int getPerfDeltaTimeUsec(const TimeVal start, const TimeVal end)
{
	static __int64 freq = 0;
	if (freq == 0)
		QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
	__int64 elapsed = end - start;
	return (int)(elapsed*1000000 / freq);
}

#else

// Linux, BSD, OSX

#include <sys/time.h>

TimeVal getPerfTime()
{
	timeval now;
	gettimeofday(&now, 0);
	return (TimeVal)now.tv_sec*1000000L + (TimeVal)now.tv_usec;
}

int getPerfDeltaTimeUsec(const TimeVal start, const TimeVal end)
{
	return (int)(end - start);
}

#endif
*/
namespace CasaEngine
{

GameTime::GameTime()
{
	m_ElapsedTime = 0;
	m_TotalElapsedTime = 0;
	//m_FirstStartTime = getPerfTime();
	//m_FirstStartTime = Timer::GetSystemTime();
}


GameTime::~GameTime()
{
}

/// 
///  @note
/// @return 
float GameTime::FrameTime() const
{
	//TimeVal endTime = getPerfTime();
	//return static_cast<float>(getPerfDeltaTimeUsec(m_StartTime, endTime)) / 1000.0f;
	return m_ElapsedTime;
}

/// 
///  @note
/// @return 
float GameTime::TotalTime() const
{
	//TimeVal endTime = getPerfTime();
	//return static_cast<float>(getPerfDeltaTimeUsec(m_FirstStartTime, endTime)) / 1000.0f;
	return m_TotalElapsedTime;
}

/// 
///  @note
/// @return 
void GameTime::Start()
{
	//m_StartTime = getPerfTime();
	//m_StartTime = Timer::GetSystemTime();
	m_Timer.start();
}


/// 
///  @note
/// @return 
void GameTime::End()
{
	//TimeVal endTime = getPerfTime();
	//double endTime = Timer::GetSystemTime();
	//m_ElapsedTime = static_cast<float>(Timer::DiffSystemTime(m_StartTime, endTime)) / 1000.0f;
	//m_TotalElapsedTime = static_cast<float>(Timer::DiffSystemTime(m_FirstStartTime, endTime)) / 1000.0f;
	m_ElapsedTime = m_Timer.getElapsedTimeInMilliSec();
	m_TotalElapsedTime += m_ElapsedTime;

#if defined(CASAENGINE_DEBUG)

	//If we are in a breakpoint the time of the frame
	//can be very high and create some bug
	if (m_ElapsedTime > 0.05f) // 1/20 FPS
	{
		m_ElapsedTime = 1.0f / 60.0f;
	}

#endif
}

} // namespace CasaEngine
